from flask import jsonify
from flask import Blueprint, render_template
from models import DNew
from fun import *
api_index = Blueprint('index',__name__,url_prefix='/index')   #创建一个蓝图,别名为index
#执行路径为     父别名/index/函数

@api_index.route('/')
def default():
    return jsonify({'name':'张三','sex':'男','age':22})

@api_index.route('/index')
def index():
    return jsonify({'name':'李四','sex':'女','age':11})

@api_index.route('/list')
def list():
    # 排序,偏移量，显示10条
    list = DNew.query.order_by(DNew.id.desc()).offset(5).limit(10).all()
    print(list)
    data_list = list_to_json(list)
    print(data_list)
    return jsonify(data_list)


@api_index.route('/show')
def show():
    rs = DNew.query.filter(DNew.id > 1).first()  # 返回模型对象/None
    print(model_to_dict(rs))
    return jsonify(model_to_dict(rs))
    #return jsonify([{'name':'赵六','sex':'男','age':220},{'name':'赵六1','sex':'男','age':22},{'name':'赵六2','sex':'男','age':33}])

